<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use app\admin\model\product\Purity;
use app\admin\model\product\Category;
use app\admin\model\product\Material;
use app\admin\model\product\Technology;
use app\admin\model\product\Skn;
use JetBrains\PhpStorm\Internal\TentativeType;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        // $product_category = Db::name('product_category')->column('name,name as title');
        // dump($product_category);die;
        return $this->view->fetch();
    }
    //产品分类
    public function auto_cate()
    {
        $product_category = DB::name('product')->group('product_category')->column('product_category');
      
        $product_material = DB::name('product')->group('product_material')->column('product_material');
        
        if($product_material){
            $narr = [];
            foreach($product_material as $v){
                $v = explode(',',$v);
                foreach($v as $v0){
                    array_push($narr,$v0);
                }
            }
          $product_material = array_unique($narr);
        }
        $product_purity = DB::name('product')->group('product_purity')->column('product_purity');
        if($product_purity){
            $narr = [];
            foreach($product_purity as $v){
                $v = explode(',',$v);
                foreach($v as $v0){
                    array_push($narr,$v0);
                }
            }
          $product_purity = array_unique($narr);
        }
        $product_technology = DB::name('product')->group('product_technology')->column('product_technology');
        if($product_technology){
            $narr = [];
            foreach($product_technology as $v){
                $v = explode(',',$v);
                foreach($v as $v0){
                    array_push($narr,$v0);
                }
            }
          $product_technology = array_unique($narr);
        }
        $product_skn = DB::name('product')->group('product_skn')->column('product_skn');
        if($product_skn){
            $narr = [];
            foreach($product_skn as $v){
                $v = explode(',',$v);
                foreach($v as $v0){
                    array_push($narr,$v0);
                }
            }
          $product_skn = array_unique($narr);
        }
        if (!empty($product_category)) {
            $del = $new = [];
            $old = Db::name('product_category')->column('name');
            $diff = array_diff($product_category,$old);
            if ($diff) {
                foreach($diff as $v){
                    if($v){
                        $new[]['name'] = $v;
                    }
                }
                (new Category())->saveAll($new);
            }
        }
        if (!empty($product_material)) {
            $del = $new = [];
            $old = Db::name('product_material')->column('name');
            $diff = array_diff($product_material,$old);
            if ($diff) {
                foreach($diff as $v){
                   if($v){
                        $new[]['name'] = $v;
                    }
                }
                (new Material())->saveAll($new);
            }
        }
        if (!empty($product_purity)) {
            $del = $new = [];
            $old = Db::name('product_purity')->column('name');
            $diff = array_diff($product_purity,$old);
            if ($diff) {
                foreach($diff as $v){
                   if($v){
                        $new[]['name'] = $v;
                    }
                }
                (new Purity)->saveAll($new);
            }
        }
        if (!empty($product_technology)) {
            $del = $new = [];
            $old = Db::name('product_technology')->column('name');
            $diff = array_diff($product_technology,$old);
            if ($diff) {
                foreach($diff as $v){
                   if($v){
                        $new[]['name'] = $v;
                    }
                }
                (new Technology())->saveAll($new);
            }
        }
        if (!empty($product_skn)) {
            $del =  $new = [];
            $old = Db::name('product_skn')->column('name');
            $diff = array_diff($product_skn,$old);
            if ($diff) {
                foreach($diff as $v){
                   if($v){
                        $new[]['name'] = $v;
                    }
                }
                (new Skn())->saveAll($new);
            }
        }
        return json(['code'=>1]);
    }
}
